package org.simple.framework.boot.controller;

import org.simple.framework.core.util.Dict;
import org.simple.framework.data.entity.Identifiable;
import org.simple.framework.web.result.base.SimpleResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import jakarta.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.security.Principal;

/**
 * 项目名称：simple-framework
 * 类名称：ModelCreateController
 * 类描述：ModelCreateController
 * 创建时间：2018/9/7
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
public interface ModelCreateController<E extends Identifiable<ID>, ID extends Serializable, M> extends ModelController<E, ID> {

    E handleCreate(M model, Principal principal, HttpServletRequest request);

    @PostMapping
    default SimpleResult create(@RequestBody M model, Principal principal, HttpServletRequest request) {
        E entity = handleCreate(model, principal, request);
        getRepository().save(entity);

        return SimpleResult.ok(Dict.create().set("id", entity.getId()));
    }

}
